﻿using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CastingWellEquipmentGUI.ViewModel.Modal.CastWellPublicAuxiliary
{
    /// <summary>
    /// 触屏数字键盘的交互逻辑 基于MVVMLight
    /// </summary>
    public class SetCVPanelViewModel : ViewModelBase
    {
        /// <summary>
        /// 监控的标签列表
        /// </summary>
        private ObservableCollection<MonitoreTagModel> _MonitoreTags;
        /// <summary>
        /// 监控的标签列表
        /// </summary>
        public ObservableCollection<MonitoreTagModel> MonitoreTags
        {
            get { return _MonitoreTags; }
            set
            {
                _MonitoreTags = value;
                RaisePropertyChanged("MonitoreTags");
            }
        }

        /// <summary>
        /// 输入的内容
        /// </summary>
        private string _NumberValue;
        /// <summary>
        /// 输入的内容
        /// </summary>
        public string NumberValue
        {
            get { return _NumberValue; }
            set
            {
                _NumberValue = value;
                RaisePropertyChanged("NumberValue");
            }
        }
        /// <summary>
        /// 关闭当前窗体命令
        /// </summary>
        public DelegateCommand CloseAppCommand { get; set; }
        /// <summary>
        /// 输入命令
        /// </summary>
        public DelegateCommand InputCommand { get; set; }
        private string CallbackFuncation;
        /// <summary>
        /// 构造函数 这里初始化了关闭和输入命令
        /// </summary>
        public SetCVPanelViewModel()
        {
            //初始化关闭命令
            CloseAppCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CloseAppHandler)
            };
            //初始化输入命令
            InputCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(InputHandler)
            };
        }
        /// <summary>
        /// 提供给调用者用来设置回调消息名称
        /// </summary>
        /// <param name="CallbackFuncationName">回调消息令牌名称</param>
        public void SetCallbackFuncationName(string CallbackFuncationName)
        {
            CallbackFuncation = CallbackFuncationName;
        }
        /// <summary>
        /// 提供给调用者用来设置已输入内容的默认值的方法
        /// </summary>
        /// <param name="currentVal">要初始化到当前逻辑中的已输入内容</param>
        public void SetCurrentVal(string currentVal)
        {
            if (currentVal == null)
            {
                currentVal = "0";
            }
            NumberValue = currentVal;
        }
        /// <summary>
        /// 退出程序命令处理程序
        /// </summary>
        /// <param name="obj">参数</param>
        private void CloseAppHandler(object obj)
        {
            NumberValue = "0";
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideSetCVPanel"));
        }
        /// <summary>
        /// 输入命令解析
        /// 采用解释器设计模式
        /// </summary>
        /// <param name="obj">输入的内容</param>
        private void InputHandler(object obj)
        {
            switch (obj.ToString())
            {
                //如果是阿拉伯数字
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                case "0":
                    //如果已输入的数包含小数点
                    if (NumberValue.Contains("."))
                    {
                        string IntegerPart = NumberValue.Substring(0, NumberValue.IndexOf("."));
                        string FractionalPart = NumberValue.Substring(NumberValue.IndexOf("."));
                        NumberValue = Double.Parse(IntegerPart) + FractionalPart + obj.ToString();
                    }
                    //如果已输入的数不包含小数点
                    else
                    {
                        NumberValue = Double.Parse(NumberValue + obj.ToString()).ToString();
                    }
                    break;
                //如果是小数点
                case "DecimalPoint":
                    if (!NumberValue.Contains("."))
                    {
                        NumberValue = NumberValue + ".";
                    }
                    break;
                //如果是清屏
                case "C":
                    NumberValue = "0";
                    break;
                //如果是平方
                case "Square":
                    //如果不等于0
                    if (Double.Parse(NumberValue) != 0)
                    {
                        NumberValue = Math.Pow(Double.Parse(NumberValue), 2).ToString();
                    }
                    break;
                //如果是删除
                case "←":
                    //如果长度大于2
                    if (NumberValue.Length - 1 > 0)
                    {
                        NumberValue = NumberValue.Substring(0, NumberValue.Length - 1);
                    }
                    else
                    {
                        NumberValue = "0";
                    }
                    break;
                //如果是确认
                case "Enter":
                    //如果最后一个字符是小数
                    if (NumberValue.Contains("."))
                    {
                        string DecimalPoint = NumberValue.ToCharArray()[NumberValue.Length - 1].ToString();
                        if (DecimalPoint == ".")
                        {
                            NumberValue = NumberValue.Substring(0, NumberValue.Length - 1);
                        }
                    }
                    NumberValue = Double.Parse(NumberValue).ToString();
                    //发送结果
                    Messenger.Default.Send<string>(NumberValue, CallbackFuncation);
                    //清除掉输入内容 设置为0
                    NumberValue = "0";
                    //发送隐藏本窗体
                    Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideSetCVPanel"));
                    break;

            }
        }
    }
}
